package indi.zjh.dp.vc;

import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.List;

import org.dom4j.DocumentException;

import indi.zjh.dp.db.Configuration;
import indi.zjh.dp.db.Session;
import indi.zjh.dp.db.TField;
import indi.zjh.dp.db.Table;
import indi.zjh.dp.orm.BusinessMapDataStrategy;
import indi.zjh.dp.orm.RelationClass;
import indi.zjh.dp.vc.Version;

public class Upgrade {
	public static void processNewBusiness(Version oldVersion, Version newVersion, RelationClass rc) throws DocumentException, Exception
	{
		List<TField> tFields = BusinessMapDataStrategy.getDataField(rc);
		Table table = new Table(BusinessMapDataStrategy.getTableName(rc));
		table.addField(tFields);
		
		
		Session session = Configuration.getSessionFactory().createSession();
		session.alter(table.getCreateSql());
		session.close();
	}
	
	public static void processNewField(Version oldVersion, Version newVersion, RelationClass rc, Field newField) throws SQLException, DocumentException, Exception
	{
		String tableName = BusinessMapDataStrategy.getTableName(rc);
		TField tf = BusinessMapDataStrategy.getDataField(newField);
		String sql = "ALTER TABLE `"+tableName+"` ADD "+tf.getString();

		Session session = Configuration.getSessionFactory().createSession();
		session.alter(sql);
		session.close();
	}
}
